#include "angle.h"
#include "REG.h"

/* USER CODE END 0 */

UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;
UART_Data_t stuHwtUart;
HWT_Reg_t stuHwtReg;
// ALIGN_32BYTES (UART_Data_t stuHwtUart) __attribute__((section(".ARM.__at_0x24000000")));
/* USART3 init function */


/// @brief USART1 DMA Receive Init
/// @param baudrate Baud rate
void HWT_USART1_Init(uint32_t baudrate)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* USER CODE BEGIN USART3_MspInit 0 */

  /* USER CODE END USART3_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
    /* GPIOB clock enable */
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /* DMA1 clock enable */
    __HAL_RCC_DMA1_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PB14     ------> USART1_TX
    PB15     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

    /* USART1 DMA Init */
    hdma_usart1_rx.Instance = DMA1_Stream0;
    hdma_usart1_rx.Init.Request = DMA_REQUEST_USART1_RX;
    hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
    hdma_usart1_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    hdma_usart1_rx.Init.MemBurst = DMA_MBURST_SINGLE;                 //存储器突发单次传输
    hdma_usart1_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;              //外设突发单次传输
    if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);

    /* NVIC中设置DMA中断优先级*/
    HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 5, 0);
    /* NVIC中使能DMA中断*/
    HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);

    /* USART1_RX Init */
    huart1.Instance        = USART1;                               // 串口号
    huart1.Init.BaudRate   = baudrate;                             // 波特率
    huart1.Init.WordLength = UART_WORDLENGTH_8B;                   // 有效数据长度
    huart1.Init.StopBits   = UART_STOPBITS_1;                      // 停止位
    huart1.Init.Parity     = UART_PARITY_NONE;                     // 奇偶校验
    huart1.Init.Mode       = UART_MODE_TX_RX;                      // 发送&接收模式
    huart1.Init.HwFlowCtl  = UART_HWCONTROL_NONE;                  // 硬件流控制
    huart1.Init.OverSampling    = UART_OVERSAMPLING_16;            // 过采样
    huart1.Init.OneBitSampling  = UART_ONE_BIT_SAMPLE_DISABLE;     // 采样位
    huart1.Init.Prescaler       = UART_PRESCALER_DIV1;             // 预分频器系数
    huart1.Init.FIFOMode        = UART_FIFOMODE_DISABLE;           // FIFO模式
    huart1.Init.TXFIFOThreshold = UART_TXFIFO_THRESHOLD_1_8;       // 发送FIFO阈值
    huart1.Init.RXFIFOThreshold = UART_RXFIFO_THRESHOLD_1_8;       // 接收FIFO阈值  
    huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;  // 高级应用功能
    HAL_UART_Init(&huart1);

    __HAL_UART_CLEAR_IT(&huart1,USART_ICR_RTOCF);
	  __HAL_UART_CLEAR_IDLEFLAG(&huart1);
    __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);//开启接收中断
    
    __HAL_UART_DISABLE_IT(&huart1, UART_IT_ERR);
    __HAL_UART_DISABLE_IT(&huart1, UART_IT_PE);
    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);

    HAL_UART_Receive_DMA(&huart1,stuHwtUart.Rx_buf,USART_DMA_RECV_LEN);
}

/// @brief Hwt101 Modbus Read function
/// @param reg read start register
/// @param length read length
void Hwt_Read_Reg(uint8_t reg,uint8_t length)
{
    uint16_t Crc;
    stuHwtReg.readReg = reg;
    stuHwtReg.readLen = length;
    stuHwtUart.Tx_buf[0] = HWT101_ADDRESS;
    stuHwtUart.Tx_buf[1] = MODBUS_CODE_READ;
    stuHwtUart.Tx_buf[2] = 0x00;
    stuHwtUart.Tx_buf[3] = reg;
    stuHwtUart.Tx_buf[4] = 0x00;
    stuHwtUart.Tx_buf[5] = length;
    Crc = usMBCRC16(stuHwtUart.Tx_buf,6);
    stuHwtUart.Tx_buf[6] = Crc & 0xFF;
    stuHwtUart.Tx_buf[7] = Crc >> 8;
    stuHwtUart.Tx_len = 8;
    HAL_UART_Transmit(&huart1,stuHwtUart.Tx_buf,stuHwtUart.Tx_len,1000);
}

/// @brief Hwt101 Modbus Write function
/// @param reg write register
/// @param data write data
void Hwt_Write_Reg(uint8_t reg,int16_t data)
{
    uint16_t Crc;
    stuHwtReg.writeReg = reg;
    stuHwtUart.Tx_buf[0] = HWT101_ADDRESS;
    stuHwtUart.Tx_buf[1] = MODBUS_CODE_WRITE;
    stuHwtUart.Tx_buf[2] = 0x00;
    stuHwtUart.Tx_buf[3] = reg;
    stuHwtUart.Tx_buf[4] = data >> 8;
    stuHwtUart.Tx_buf[5] = data & 0xFF;
    Crc = usMBCRC16(stuHwtUart.Tx_buf,6);
    stuHwtUart.Tx_buf[6] = Crc & 0xFF;
    stuHwtUart.Tx_buf[7] = Crc >> 8;
    stuHwtUart.Tx_len = 8;
    HAL_UART_Transmit(&huart1,stuHwtUart.Tx_buf,stuHwtUart.Tx_len,1000);
}

/// @brief HWT101 Z轴置零位
/// @param  
void Hwt_Yaw_Clear(void)
{
  // HAL_Delay(1000);
  Hwt_Write_Reg(KEY,KEY_UNLOCK);
  HAL_Delay(200);
  Hwt_Write_Reg(XRSCALE,0x00);
  HAL_Delay(200);
  Hwt_Write_Reg(SAVE,SAVE_PARAM);
  HAL_Delay(200);
}

unsigned short usMBCRC16(unsigned char *pu8Buf, unsigned char u8Len) 
{
	unsigned short u16Crc = 0xFFFF;
	unsigned char u8Index = 0;
	unsigned char u8Cnt = 0;
	unsigned char u8CrcH= 0 ;
	unsigned char u8CrcL= 0 ;
	for(u8Index = 0;u8Index < u8Len; u8Index++)
	{
		u8CrcH = (unsigned char)((u16Crc & 0xFF00) >> 8);
		u8CrcL = (unsigned char)((u16Crc & 0x00FF));
		u8CrcL = u8CrcL ^ pu8Buf[u8Index];
		u16Crc = ((unsigned short)u8CrcH) << 8 | u8CrcL;
		for(u8Cnt = 0; u8Cnt < 8; u8Cnt++ )
		{
			if((u16Crc & 0x0001) == 0x0001)
			{
				u16Crc >>= 1;
				u16Crc = u16Crc ^ 0xa001; 
			}
			else
			{
				u16Crc >>= 1; 
			}
		}
	}
	return u16Crc; 
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
